var app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderUser: '无',
    orderTel: '无',
    orderAddress: '无',
    postImages:'',
    totalPrice:'',
    myhost:app.globalData.remoteDomainMy
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //获取收货人信息
    this.getUserInfo();
    this.cartContent();
  },

  /**
   * 获取收货人信息
   */
  getUserInfo: function() {
    var that = this

    wx.request({
      url: app.globalData.remoteDomainApi + 'getuserinfo.php',
      data: {
        openid: app.globalData.openid,
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if (res.data == null) { //表明该用户还没有填写资料
          wx.showModal({
            title: '提示',
            content: '请完善您的个人资料',
            success(res) {
              if (res.confirm) { //跳转到完善个人资料页面
                wx.navigateTo({
                  url: '../myinfo/myinfo',
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })

        } else { //渲染数据到模板
          that.setData({
            orderUser: res.data.uname,
            orderTel: res.data.tel,
            orderAddress: res.data.address
          })
        }
      }
    })
  },

  /**
   * 所购产品
   */
  cartContent:function(){

    //定义临时数组
    var postArr = [];

    //定义价格
    var price = 0;

    //从storage中读取
    const products = wx.getStorageSync(app.globalData.openid);

    //读取封面
    for (var i in products){

      products[i].forEach(function(item){

        postArr.push(item.pdatas.coverurl);

        price += parseFloat(item.pdatas.price * item.count)

      })
    }

    this.setData({
      postImages: postArr,
      totalPrice:price,
    })
  },

  /**
   * 提交订单
   */
  submitOrder: function() {
    //校验收货人
    this.getUserInfo();

    //执行提交

    if(this.data.orderTel != '无' && this.data.totalPrice>0){//确保填写了联系人和购买了产品

      wx.request({
        url: app.globalData.remoteDomainApi+'createorder.php', 
        data: {
          openid: app.globalData.openid,
          datas: JSON.stringify(wx.getStorageSync(app.globalData.openid)
          )
          },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success(res) {          
          if(res.data == 2){

            // 销毁storage
            wx.setStorageSync(app.globalData.openid,{book:[],music:[],movie:[]})

            // 跳转
            wx.reLaunch({
              url: '../my/my',
            })
            
          }else{
            console.log('订单提交失败')
          }
        }
      })


    }
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;

    wx.request({
      url: app.globalData.remoteDomainApi + 'getuserinfo.php',
      data: {
        openid: app.globalData.openid,
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if (res.data != null) {
          that.setData({
            orderUser: res.data.uname,
            orderTel: res.data.tel,
            orderAddress: res.data.address
          })
        }
      }
    })    
  }

 
})